package com.my.oschina.adapter;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.my.oschina.bean.TweetList;
import com.my.oschina.common.AppFragmentPagerAdapter;
import com.my.oschina.fragment.TweetFragment;

/**
 * 动弹Tweet-PagerAdaper，继承了AppFragmentPagerAdapter，后者又继承了PagerAdapter
 */
public class TweetFragmentPagerAdapter extends AppFragmentPagerAdapter {

	private List<Fragment> fragmentList;
	private static int TWE_TAB_SIZE = 3;
	private String[] tagString = { "tag0", "tag1", "tag2" };

	/**
	 * 
	 * @param tf
	 * @param viewPager
	 * @param fm
	 * @param activity
	 * @param fragmentList
	 * @param catlog
	 */
	public TweetFragmentPagerAdapter(TweetFragment tf, ViewPager viewPager,
			FragmentManager fm, List<Fragment> fragmentList, int catlog) {

		super(fm);
		this.fragmentList = fragmentList;

		fragmentList
				.add(0, tf.getTweetChildFragment(TweetList.CATALOG_LASTEST));
		fragmentList.add(1, tf.getTweetChildFragment(TweetList.CATALOG_HOT));
		fragmentList.add(2, tf.getTweetChildFragment(catlog));

		FragmentTransaction ft = tf.getChildFragmentManager()
				.beginTransaction();

		for (int i = 0; i < TWE_TAB_SIZE; i++) {
			if (fragmentList.get(i).isAdded()) {
				ft.add(viewPager.getId(), fragmentList.get(i), tagString[i]);
			}
		}

		if (!ft.isEmpty()) {
			ft.commit();
			tf.getChildFragmentManager().executePendingTransactions();
		}
	}

	@Override
	public Fragment getItem(int position) {
		return fragmentList.get(position);
	}

	@Override
	protected String getTag(int position) {
		List<String> tagList = new ArrayList<String>();

		for (int i = 0; i < TWE_TAB_SIZE; i++) {
			tagList.add(tagString[i]);
		}

		return tagList.get(position);
	}

	@Override
	public int getCount() {
		return TWE_TAB_SIZE;
	}

}
